import axios from '../index'

/**
 * 设置Tab API管理器
 */
class SettingsTabManager {
    constructor() {
        this.baseUrl = '/settings'
    }

    // ========== 本地网络设置 CRUD ==========
    async saveLocalNetworkSettings(data) {
        console.log('💾 [TabManager] 保存本地网络设置', { data })
        try {
            const response = await axios.put(`${this.baseUrl}/local-network`, data)
            console.log('✅ [TabManager] 保存本地网络设置成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 保存本地网络设置失败:', error)
            throw error
        }
    }

    async getLocalNetworkSettings() {
        console.log('🔍 [TabManager] 获取本地网络设置')
        try {
            const response = await axios.get(`${this.baseUrl}/local-network`)
            console.log('✅ [TabManager] 获取本地网络设置成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取本地网络设置失败:', error)
            throw error
        }
    }

    async resetLocalNetworkSettings() {
        console.log('🔄 [TabManager] 重置本地网络设置')
        try {
            const response = await axios.post(`${this.baseUrl}/local-network/reset`)
            console.log('✅ [TabManager] 重置本地网络设置成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 重置本地网络设置失败:', error)
            throw error
        }
    }

    // ========== HJ212网络设置 CRUD ==========
    async saveHj212NetworkSettings(data) {
        console.log('💾 [TabManager] 保存HJ212网络设置', { data })
        try {
            const response = await axios.put(`${this.baseUrl}/hj212-network`, data)
            console.log('✅ [TabManager] 保存HJ212网络设置成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 保存HJ212网络设置失败:', error)
            throw error
        }
    }

    async getHj212NetworkSettings() {
        console.log('🔍 [TabManager] 获取HJ212网络设置')
        try {
            const response = await axios.get(`${this.baseUrl}/hj212-network`)
            console.log('✅ [TabManager] 获取HJ212网络设置成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取HJ212网络设置失败:', error)
            throw error
        }
    }

    async getHj212SystemType() {
        console.log('🔍 [TabManager] 获取HJ212系统类型')
        try {
            const response = await axios.get(`${this.baseUrl}/hj212-system-type`)
            console.log('✅ [TabManager] 获取HJ212系统类型成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取HJ212系统类型失败:', error)
            throw error
        }
    }

    // ========== 花生壳配置 CRUD ==========
    async saveOrayConfig(data) {
        console.log('💾 [TabManager] 保存花生壳配置', { data })
        try {
            const response = await axios.put(`${this.baseUrl}/oray-config`, data)
            console.log('✅ [TabManager] 保存花生壳配置成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 保存花生壳配置失败:', error)
            throw error
        }
    }

    async getOrayConfig() {
        console.log('🔍 [TabManager] 获取花生壳配置')
        try {
            const response = await axios.get(`${this.baseUrl}/oray-config`)
            console.log('✅ [TabManager] 获取花生壳配置成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取花生壳配置失败:', error)
            throw error
        }
    }

    async resetOrayConfig() {
        console.log('🔄 [TabManager] 重置花生壳配置')
        try {
            const response = await axios.post(`${this.baseUrl}/oray-config/reset`)
            console.log('✅ [TabManager] 重置花生壳配置成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 重置花生壳配置失败:', error)
            throw error
        }
    }

    // 花生壳操作
    async getOraySN(data) {
        console.log('🔍 [TabManager] 获取花生壳SN', { data })
        try {
            const response = await axios.get(`${this.baseUrl}/oray/sn`, { params: data })
            console.log('✅ [TabManager] 获取花生壳SN成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取花生壳SN失败:', error)
            throw error
        }
    }

    async getOrayStatus(data) {
        console.log('🔍 [TabManager] 获取花生壳运行状态', { data })
        try {
            const response = await axios.get(`${this.baseUrl}/oray/status`, { params: data })
            console.log('✅ [TabManager] 获取花生壳运行状态成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取花生壳运行状态失败:', error)
            throw error
        }
    }

    async restartOray(data) {
        console.log('🔄 [TabManager] 重启花生壳', { data })
        try {
            const response = await axios.get(`${this.baseUrl}/oray/restart`, { params: data })
            console.log('✅ [TabManager] 重启花生壳成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 重启花生壳失败:', error)
            throw error
        }
    }

    async resetOray(data) {
        console.log('🔄 [TabManager] 重置花生壳', { data })
        try {
            const response = await axios.get(`${this.baseUrl}/oray/reset`, { params: data })
            console.log('✅ [TabManager] 重置花生壳成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 重置花生壳失败:', error)
            throw error
        }
    }

    async startOray(data) {
        console.log('▶️ [TabManager] 启动花生壳', { data })
        try {
            const response = await axios.get(`${this.baseUrl}/oray/start`, { params: data })
            console.log('✅ [TabManager] 启动花生壳成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 启动花生壳失败:', error)
            throw error
        }
    }

    async stopOray(data) {
        console.log('⏹️ [TabManager] 停止花生壳', { data })
        try {
            const response = await axios.get(`${this.baseUrl}/oray/stop`, { params: data })
            console.log('✅ [TabManager] 停止花生壳成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 停止花生壳失败:', error)
            throw error
        }
    }

    // ========== MQTT网络设置 CRUD ==========
    async saveMqttNetworkSettings(data) {
        console.log('💾 [TabManager] 保存MQTT网络设置', { data })
        try {
            const response = await axios.put(`${this.baseUrl}/mqtt-network`, data)
            console.log('✅ [TabManager] 保存MQTT网络设置成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 保存MQTT网络设置失败:', error)
            throw error
        }
    }

    async getMqttNetworkSettings() {
        console.log('🔍 [TabManager] 获取MQTT网络设置')
        try {
            const response = await axios.get(`${this.baseUrl}/mqtt-network`)
            console.log('✅ [TabManager] 获取MQTT网络设置成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取MQTT网络设置失败:', error)
            throw error
        }
    }

    // ========== MQTT配置项管理 CRUD ==========

    async getMqttUIConfig() {
        console.log('🔍 [TabManager] 获取MQTT配置项')
        try {
            const response = await axios.get(`${this.baseUrl}/mqtt-network/ui-config`)
            console.log('✅ [TabManager] 获取MQTT配置项成功')
            return response
        } catch (error) {   
            console.error('❌ [TabManager] 获取MQTT配置项失败:', error)
            throw error
        }
    }

    async getMqttCustomTopicList() {
        console.log('🔍 [TabManager] 获取MQTT自定义主题列表')
        try {
            const response = await axios.get(`${this.baseUrl}/mqtt-network/custom-topic`)
            console.log('✅ [TabManager] 获取MQTT自定义主题列表成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取MQTT自定义主题列表失败:', error)
            throw error
        }
    }

    // 获取所有MQTT配置列表
    async getMqttConfigList() {
        console.log('🔍 [TabManager] 获取MQTT配置列表')
        try {
            const response = await axios.get(`${this.baseUrl}/mqtt-network/configs`)
            console.log('✅ [TabManager] 获取MQTT配置列表成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取MQTT配置列表失败:', error)
            throw error
        }
    }

    // 获取单个MQTT配置
    async getMqttConfig(configId) {
        console.log('🔍 [TabManager] 获取MQTT配置', { configId })
        try {
            const response = await axios.get(`${this.baseUrl}/mqtt-network/configs/${configId}`)
            console.log('✅ [TabManager] 获取MQTT配置成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 获取MQTT配置失败:', error)
            throw error
        }
    }

    // 添加单个MQTT配置
    async addMqttConfig(data) {
        console.log('➕ [TabManager] 添加MQTT配置', { data })
        try {
            const response = await axios.post(`${this.baseUrl}/mqtt-network/configs`, data)
            console.log('✅ [TabManager] 添加MQTT配置成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 添加MQTT配置失败:', error)
            throw error
        }
    }

    // 更新单个MQTT配置
    async updateMqttConfig(configId, data) {
        console.log('✏️ [TabManager] 更新MQTT配置', { configId, data })
        try {
            const response = await axios.put(`${this.baseUrl}/mqtt-network/configs/${configId}`, data)
            console.log('✅ [TabManager] 更新MQTT配置成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 更新MQTT配置失败:', error)
            throw error
        }
    }

    // 删除单个MQTT配置
    async deleteMqttConfig(configId) {
        console.log('🗑️ [TabManager] 删除MQTT配置', { configId })
        try {
            const response = await axios.delete(`${this.baseUrl}/mqtt-network/configs/${configId}`)
            console.log('✅ [TabManager] 删除MQTT配置成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 删除MQTT配置失败:', error)
            throw error
        }
    }

    // 批量删除MQTT配置
    async batchDeleteMqttConfigs(configIds) {
        console.log('🗑️ [TabManager] 批量删除MQTT配置', { configIds })
        try {
            const response = await axios.delete(`${this.baseUrl}/mqtt-network/configs/batch`, { 
                data: { ids: configIds } 
            })
            console.log('✅ [TabManager] 批量删除MQTT配置成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 批量删除MQTT配置失败:', error)
            throw error
        }
    }

    // 复制MQTT配置
    async copyMqttConfig(configId, newTitle) {
        console.log('📋 [TabManager] 复制MQTT配置', { configId, newTitle })
        try {
            const response = await axios.post(`${this.baseUrl}/mqtt-network/configs/${configId}/copy`, { 
                title: newTitle 
            })
            console.log('✅ [TabManager] 复制MQTT配置成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 复制MQTT配置失败:', error)
            throw error
        }
    }

    // 重置MQTT配置
    async resetMqttConfig(configId) {
        console.log('🔄 [TabManager] 重置MQTT配置', { configId })
        try {
            const response = await axios.post(`${this.baseUrl}/mqtt-network/configs/${configId}/reset`)
            console.log('✅ [TabManager] 重置MQTT配置成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 重置MQTT配置失败:', error)
            throw error
        }
    }

    // ========== 批量操作 ==========
    // 获取所有设置数据 - 切换分组设备时使用
    async getAllSettingsData() {
        console.log('🔍 [TabManager] 获取所有设置数据')
        try {
            const response = await axios.get(`${this.baseUrl}/all`)
            console.log('✅ [TabManager] 获取所有设置数据成功', response.data)
            return response.data
        } catch (error) {
            console.error('❌ [TabManager] 获取所有设置数据失败:', error)
            throw error
        }
    }

    async getAllNetworkMqttSettings() {
        console.log('🔍 [TabManager] 获取所有MQTT网络设置')
        try {
            const response = await axios.get(`${this.baseUrl}/mqtt-network/all`)
            console.log('✅ [TabManager] 获取所有MQTT网络设置成功')
            return response.data
        } catch (error) {
            console.error('❌ [TabManager] 获取所有MQTT网络设置失败:', error)
            throw error
        }
    }
    
    // 批量更新所有设置数据
    async updateAllSettingsData(data) {
        console.log('🔄 [TabManager] 批量更新所有设置数据', { data })
        try {
            const response = await axios.put(`${this.baseUrl}/all`, data)
            console.log('✅ [TabManager] 批量更新所有设置数据完成', response.data)
            return response.data
        } catch (error) {
            console.error('❌ [TabManager] 批量更新所有设置数据失败:', error)
            throw error
        }
    }

    // 重置所有设置数据
    async resetAllSettingsData() {
        console.log('🔄 [TabManager] 重置所有设置数据')
        try {
            const response = await axios.post(`${this.baseUrl}/all/reset`)
            console.log('✅ [TabManager] 重置所有设置数据成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 重置所有设置数据失败:', error)
            throw error
        }
    }

    // 导出所有设置数据
    async exportAllSettingsData() {
        console.log('📤 [TabManager] 导出所有设置数据')
        try {
            const response = await axios.get(`${this.baseUrl}/all/export`)
            console.log('✅ [TabManager] 导出所有设置数据成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 导出所有设置数据失败:', error)
            throw error
        }
    }

    // 导入所有设置数据
    async importAllSettingsData(data) {
        console.log('📥 [TabManager] 导入所有设置数据', { data })
        try {
            const response = await axios.post(`${this.baseUrl}/all/import`, data)
            console.log('✅ [TabManager] 导入所有设置数据成功')
            return response
        } catch (error) {
            console.error('❌ [TabManager] 导入所有设置数据失败:', error)
            throw error
        }
    }
}

export default SettingsTabManager 